//
//  YMMainSheetBaseView.m
//  YMMedical
//
//  Created by y s on 2023/7/20.
//

#import "YMMainSheetBaseView.h"

#define kSheet_height ([SFitUtil fit:304]+IPHONE_X_BOTTOM_HEIGHT)

@interface YMMainSheetBaseView ()

@property (nonatomic, strong) UIView *vSheet;
@property (nonatomic, strong) UIView *vTop;
@property (nonatomic, strong) UIView *vLine;
@property (nonatomic, strong) UILabel *lblTitle;

@property (nonatomic, strong) UIButton *btnCancle;
@property (nonatomic, strong) UIButton *btnSure;

@end

@implementation YMMainSheetBaseView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self initUI];
    }
    return self;
}

#pragma mark -- UI
- (void)initUI{
    self.backgroundColor = [SUIUtil colorOf:0x000000 alpha:0.5];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self addSubview:self.vSheet];
    [self.vSheet addSubview:self.vTop];
    [self.vTop addSubview:self.btnSure];
    [self.vTop addSubview:self.btnCancle];
    [self.vTop addSubview:self.lblTitle];
    [self.vTop addSubview:self.vLine];
    [self.vSheet addSubview:self.vBg];
    
    self.vSheet.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, kSheet_height);
    
    [self.vTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo([SFitUtil fit:45]);
    }];
    [self.vLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo([SFitUtil fitLine]);
    }];
    [self.btnCancle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.mas_equalTo(0);
        make.width.mas_equalTo([SFitUtil fit:24+28]);
    }];
    [self.btnSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.width.mas_equalTo([SFitUtil fit:24+28]);
    }];
    [self.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.vTop.mas_bottom);
        make.bottom.left.right.mas_equalTo(0);
    }];
}

- (void)show{
    
    UIWindow *keyWindow = [SThemeUtil keyWindow];
    for (id item in keyWindow.subviews) {
        if ([item isKindOfClass:[self class]]) {
            return;
        }
    }
    [keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.vSheet.frame = CGRectMake(0, SCREEN_HEIGHT-kSheet_height, SCREEN_WIDTH, kSheet_height);
    }];
}
- (void)dismiss{
    [UIView animateWithDuration:0.3 animations:^{
        self.vSheet.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, kSheet_height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)setStrTitle:(NSString *)strTitle{
    _strTitle = strTitle;
    self.lblTitle.text = strTitle;
}

- (void)sureActionClick{
    if (self.sureClickBlock) {
        self.sureClickBlock();
    }
    [self dismiss];
}
- (void)cancelActionClick{
    if (self.cancelClickBlock) {
        self.cancelClickBlock();
    }
    [self dismiss];
}

#pragma mark -- setter and getter
- (UIView *)vSheet{
    if (_vSheet == nil) {
        _vSheet = [[UIView alloc] init];
    }
    return _vSheet;
}
- (UIView *)vTop{
    if (_vTop == nil) {
        _vTop = [[UIView alloc] init];
        _vTop.backgroundColor = STheme.D9;
        CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, [SFitUtil fit:45]);
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:[SFitUtil fitSize:16 height:16]];
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = frame;
        layer.path = path.CGPath;
        _vTop.layer.mask = layer;
    }
    return _vTop;
}
- (UIView *)vBg{
    if (_vBg == nil) {
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = STheme.D9;
    }
    return _vBg;
}
- (UIView *)vLine {
    if(_vLine == nil) {
        _vLine = [[UIView alloc] init];
        _vLine.backgroundColor = STheme.LINE_COLOR;
    }
    return _vLine;
}
- (UIButton *)btnSure{
    if (_btnSure == nil) {
        _btnSure = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnSure.titleLabel.font = [SFitUtil fitFont:14];
        [_btnSure setTitleColor:STheme.D13 forState:UIControlStateNormal];
        [_btnSure setTitle:KLanguage(@"sure_txt_key") forState:UIControlStateNormal];
        [_btnSure addTarget:self action:@selector(sureActionClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnSure;
}
- (UIButton *)btnCancle{
    if (_btnCancle == nil) {
        _btnCancle = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnCancle.titleLabel.font = [SFitUtil fitFont:14];
        [_btnCancle setTitleColor:STheme.D13 forState:UIControlStateNormal];
        [_btnCancle setTitle:KLanguage(@"cancle_txt") forState:UIControlStateNormal];
        [_btnCancle addTarget:self action:@selector(cancelActionClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnCancle;
}
- (UILabel *)lblTitle{
    if (_lblTitle == nil){
        _lblTitle = [SThemeUtil label:@"" color:STheme.H_D1 fontSize:14];
    }
    return _lblTitle;
}

@end
